﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LOLServer.Biz.Account.Interface;
using LOLServer.Biz.User.Interface;
using LOLServer.Cache;
using LOLServer.Cache.Interface;
using LOLServer.DAO.Model;
using NetFrame;

namespace LOLServer.Biz.User.Impl
{
    /// <summary>
    /// 用户事务处理层
    /// </summary>
    public class UserBiz : IUserBiz
    {
        private IAccountBiz mAB = BizFactory.mAccountBiz;
        private IUserCache mUC = CacheFactory.mUserCache;
        /// <summary>
        /// 创建召唤师
        /// </summary>
        /// <param name="token"></param>
        /// <param name="name"></param>
        public bool CreateUser(UserToken token, string name)
        {
            int accountId = mAB.GetAccountId(token);
            if (accountId != 0)//说明角色在线
            {
                if (mUC.HasUserByAccountId(accountId))
                {
                    return false;
                }
                mUC.Create(token, name);
            }
            return false;
        }
        /// <summary>
        /// 通过帐号的连接对象获取 仅在初始登录验证角色时有效
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public UserModel getByAccount(UserToken token)
        {
            int accountId = mAB.GetAccountId(token);
            if (accountId==0)
            {
                return null;
            }
            return mUC.GetUserByAccountId(accountId);
        }

        /// <summary>
        /// 根据ID获取连接对象
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserToken GetConnectUser(int userId)
        {
            return mUC.GetConnectUser(userId);
        }
        /// <summary>
        /// 根据链接对象获取召唤师信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public UserModel GetUser(UserToken token)
        {
            return mUC.GetUser(token);
        }

        public UserModel GetUserByUserId(int userId)
        {
           return mUC.GetUser(userId);
        }
        /// <summary>
        /// 根据了解连接对象查看是否下线
        /// </summary>
        /// <param name="tokne"></param>
        public void OffLine(UserToken tokne)
        {
            mUC.OffLine(tokne);
        }
        /// <summary>
        /// 根据了解连接对象查看是否在线
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public UserModel OnLine(UserToken token)
        {
            int accountId = mAB.GetAccountId(token);
            if (accountId==0)
            {
                return null;
            }
            UserModel um = mUC.GetUserByAccountId(accountId);
            if (um == null) return null;
            mUC.OnLine(token, um.UserId);
           return mUC.OnLine(token,um.UserId);
        }
    }
}
